package com.ksjourney;

import mars.game.core.Config;
import mars.game.core.MarsRuntime;
import mars.game.core.MarsScreen;
import mars.game.util.Tools;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class GameUI implements MarsScreen, Config
{
	
	// ------------- UI状态-------------
	public static final int LOAD_RES = 0;
	public static final int MAIN = 1;
	public static final int HELP = 2;
	public static final int ABOUT = 3;
	public static final int SET = 4;
	
//	public static final int NEW_GAME_DAYTIME = 5;
//	public static final int NEW_GAME_NIGHT = 6;
//	public static final int NEW_GAME_DUSK = 7;
	
	public static final int SAVE = 8;
	// -----------------------
	public static final String STR_LOAD = "Loading....";
	
	int miCount = 0;
	int y = 50;
	int state;
	int end;
	
	// --------------场景循环----------------------
	// ---------------白天场景------------------
	Bitmap gameBg2 = MarsRuntime.loadBitmap(R.drawable.stage1_bg);
	Bitmap gameBg = MarsRuntime.loadBitmap(R.drawable.stage1_bg);
	Bitmap gameBgCloud = MarsRuntime.loadBitmap(R.drawable.stage1_bg2);
	Bitmap gameBgCloud2 = MarsRuntime.loadBitmap(R.drawable.stage1_bg2);
	
	int bg_x = 0;
	int bg_x2 = gameBg.getWidth();
	int bg_x_c = 0;
	int bg_x2_c = gameBg.getWidth();
	public static final int BG_SPEED = 4; // 背景移动速度
	public static final int BGCLOUD_SPEED = 10; // 背景云彩速度
	
	// ----------------夜晚场景-------------------
	Bitmap gameBgNight2 = MarsRuntime.loadBitmap(R.drawable.stage2_bg);
	Bitmap gameBgNight = MarsRuntime.loadBitmap(R.drawable.stage2_bg);
	Bitmap gameBgNIGHTTREE = MarsRuntime.loadBitmap(R.drawable.stage2_bg2);
	Bitmap gameBgNIGHTTREE2 = MarsRuntime.loadBitmap(R.drawable.stage2_bg2);
	
	// -----------------黄昏背景-------------------
	Bitmap gameBgDusk = MarsRuntime.loadBitmap(R.drawable.stage3_bg);
	Bitmap gameBgDusk2 = MarsRuntime.loadBitmap(R.drawable.stage3_bg);
	Bitmap gameBgDuskTREE = MarsRuntime.loadBitmap(R.drawable.stage3_bg2);
	Bitmap gameBgDuskTREE2 = MarsRuntime.loadBitmap(R.drawable.stage3_bg2);
	
	// ---------Loading 位置-----------------
	public static final int LOADING_BOARD_WIDTH = 2;
	public static final int POS_LODING_X = 40;
	public static final int POS_LODING_Y = SCREEN_HEIGHT - 80;
	public static final int POS_LODING_W = SCREEN_WIDTH - POS_LODING_X * 2;
	public static final int POS_LODING_H = 20;
	
	int LoadingStep;
	Paint mPaint = new Paint();// Loading0
	Rect rectStroke = new Rect(POS_LODING_X, POS_LODING_Y, POS_LODING_X + POS_LODING_W, POS_LODING_Y + POS_LODING_H); // 矩形进度外框
	Rect rectFill = new Rect(POS_LODING_X + LOADING_BOARD_WIDTH, POS_LODING_Y + LOADING_BOARD_WIDTH, POS_LODING_X, POS_LODING_Y + POS_LODING_H - LOADING_BOARD_WIDTH);// 矩形进度条
	
	// -----------------------------------------
	
	// -------开始菜单选择的位置--------
	public static final int MENU_RECT_LENGTH = 3;
	public static final int CHOOSE_LEFT = 260;
	public static final int CHOOSE_TOP = 35;
	public static final int CHOOSE_BUT_WIDTH = 100;
	public static final int CHOOSE_BUT_HEIGHT = 40;
	public static final int CHOOSE_RIGHT = CHOOSE_LEFT + CHOOSE_BUT_WIDTH;
	public static final int CHOOSE_BOTTOM = CHOOSE_TOP + CHOOSE_BUT_HEIGHT;
	public static final int SPACE_Y = 21;
	public static final int MENURECT_H = 43;
	
	public static final int CHOOSELOAD = 0;
	public static final int CHOOSESETTING = 1;
	public static final int CHOOSEQUIT = 2;
	public static final int CHOOSENEWGAME = 3;
	
	private Rect[] menuRect = new Rect[MENU_RECT_LENGTH];
	
	// -----NewGameA按键---------
	public static final int NEWGAME_BUT_LEFT = 370;
	public static final int NEWGAME_BUT_TOP = 60;
	public static final int NEWGAME_BUT_RIGHT = NEWGAME_BUT_LEFT + 104;
	public static final int NEWGAME_BUT_BUTTOM = NEWGAME_BUT_TOP + 70;
	private Rect rectNewGame = new Rect();
	
	// ---------------------------------------
	
	// -----------------设置菜单界面------------------------
	public static final int SETTING_MENU_RECT = 5;
	public static final int SET_BUT_LENGTH = 140;
	public static final int SET_BUT_HEIGHT = 40;
	public static final int SET_BUT_SPACE_Y = 10;
	public static final int SET_BUT_LEFT = 300;
	public static final int SET_BUT_TOP = 40;
	public static final int SET_BUT_RIGHT = SET_BUT_LEFT + SET_BUT_LENGTH;
	public static final int SET_BUT_BUTTOM = SET_BUT_TOP + SET_BUT_HEIGHT;
	private Rect[] SettingMenuRect = new Rect[SETTING_MENU_RECT];
	
	// ---------------------------------------------------
	// ------------------保存界面的矩形-------------------
	public static final int SAVE_MENU_RECT = 3;
	// --------左边----------
	public static final int LEFT_SAVE_BUT_LENGTH = 110;
	public static final int LEFT_SAVE_BUT_HEIGHT = 80;
	public static final int LEFT_SAVE_BUT_SPACE_Y = 5;
	public static final int LEFT_SAVE_BUT_LEFT = 20;
	public static final int LEFT_SAVE_BUT_TOP = 50;
	public static final int LEFT_SAVE_BUT_RIGHT = LEFT_SAVE_BUT_LEFT + LEFT_SAVE_BUT_LENGTH;
	public static final int LEFT_SAVE_BUT_BUTTOM = LEFT_SAVE_BUT_TOP + LEFT_SAVE_BUT_HEIGHT;
	// ---------右边----------
	public static final int RIGHT_SAVE_BUT_LENGTH = 330;
	public static final int RIGHT_SAVE_BUT_HEIGHT = 80;
	public static final int RIGHT_SAVE_BUT_SPACE_Y = 5;
	public static final int RIGHT_SAVE_BUT_LEFT = LEFT_SAVE_BUT_LENGTH + LEFT_SAVE_BUT_LEFT;
	public static final int RIGHT_SAVE_BUT_TOP = 50;
	public static final int RIGHT_SAVE_BUT_RIGHT = RIGHT_SAVE_BUT_LEFT + RIGHT_SAVE_BUT_LENGTH;
	public static final int RIGHT_SAVE_BUT_BUTTOM = RIGHT_SAVE_BUT_TOP + RIGHT_SAVE_BUT_HEIGHT;
	private Rect[] SAVEMenuRectLeft = new Rect[SAVE_MENU_RECT];
	private Rect[] SAVEMenuRectRight = new Rect[SAVE_MENU_RECT];
	
	private static final int FIRST_STAG = 0;
	private static final int SECOND_STAG = 1;
	private static final int THIRD_STAG = 2;
	
	GamePlay gp;
	
	public GameUI()
	{

		state = LOAD_RES;
		// 初始化选择菜单界面的方框
		int top = CHOOSE_TOP;
		int buttom = CHOOSE_BOTTOM;
		
		for (int i = 0; i < MENU_RECT_LENGTH; i++)
		{
			buttom = top + MENURECT_H;
			menuRect[i] = new Rect(CHOOSE_LEFT, top, CHOOSE_RIGHT, buttom);
			top += (SPACE_Y + MENURECT_H);
			
		}
		rectNewGame = new Rect(NEWGAME_BUT_LEFT, NEWGAME_BUT_TOP, NEWGAME_BUT_RIGHT, NEWGAME_BUT_BUTTOM);
		
		// 初始化设置界面的方框
		int setTop = SET_BUT_TOP;
		int setbuttom = SET_BUT_BUTTOM;
		for (int i = 0; i < SETTING_MENU_RECT; i++)
		{
			
			SettingMenuRect[i] = new Rect(SET_BUT_LEFT, setTop, SET_BUT_RIGHT, setbuttom);
			setTop += SET_BUT_SPACE_Y + SET_BUT_HEIGHT;
			setbuttom = setTop + SET_BUT_HEIGHT;
			
		}
		
		// 初始化SAVE界面的左边矩形
		int saveTop = LEFT_SAVE_BUT_TOP;
		int savebuttom = LEFT_SAVE_BUT_BUTTOM;
		for (int i = 0; i < SAVE_MENU_RECT; i++)
		{
			
			SAVEMenuRectLeft[i] = new Rect(LEFT_SAVE_BUT_LEFT, saveTop, LEFT_SAVE_BUT_RIGHT, savebuttom);
			saveTop += LEFT_SAVE_BUT_SPACE_Y + LEFT_SAVE_BUT_HEIGHT;
			savebuttom = saveTop + LEFT_SAVE_BUT_HEIGHT;
			
		}
		
		// 初始化SAVE界面的右边边矩形
		saveTop = LEFT_SAVE_BUT_TOP;
		savebuttom = LEFT_SAVE_BUT_BUTTOM;
		for (int i = 0; i < SAVE_MENU_RECT; i++)
		{
			
			SAVEMenuRectRight[i] = new Rect(RIGHT_SAVE_BUT_LEFT, saveTop, RIGHT_SAVE_BUT_RIGHT, savebuttom);
			saveTop += RIGHT_SAVE_BUT_SPACE_Y + RIGHT_SAVE_BUT_HEIGHT;
			savebuttom = saveTop + RIGHT_SAVE_BUT_HEIGHT;
			
		}
		
		//
		gp = new GamePlay(this);
	}
	
	private void drawBG(Canvas canvas)
	{

		// Paint mPaint = new Paint();
		mPaint.setColor(Color.BLACK);
		mPaint.setStyle(Style.FILL);
		// 绘制矩形--清屏作用
		canvas.drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, mPaint);
	}
	
	@Override
	public void draw(Canvas canvas)
	{

		// 绘制各个不同状态的界面
		drawBG(canvas);
		switch (state)
		{
			case LOAD_RES:
				drawRES(canvas);
				break;
			case MAIN:
				drawMAIN(canvas);
				break;
			case HELP:
				drawHELP(canvas);
				break;
			
			case ABOUT:
				drawABOUT(canvas);
				break;
			
			case SET:
				drawSET(canvas);
				break;
			
//			case NEW_GAME_DAYTIME:
//				drawNEWGAMEDAYTIME(canvas);
//				break;
//			
//			case NEW_GAME_NIGHT:
//				drawNEWGAMENIGHT(canvas);
//				break;
//			case NEW_GAME_DUSK:
//				drawNewDusk(canvas);
//				break;
			
			case SAVE:
				drawSave(canvas);
				break;
		}
	}
	
	private void drawRES(Canvas canvas)
	{

		// 描绘出LOADING界面的进度条
		mPaint.setStyle(Paint.Style.FILL);
		mPaint.setColor(Color.RED);
		canvas.drawText(STR_LOAD.substring(0, end), SCREEN_WIDTH / 2 - STR_LOAD.length() / 2, SCREEN_HEIGHT / 2, mPaint);
		
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(LOADING_BOARD_WIDTH);
		canvas.drawRect(rectStroke, mPaint);
		
		mPaint.setStyle(Paint.Style.FILL);
		mPaint.setColor(Color.YELLOW);
		canvas.drawRect(rectFill, mPaint);
	}
	
	private void drawMAIN(Canvas canvas)
	{

		// 画出选择界面的背景
		Bitmap bgMain = MarsRuntime.loadBitmap(R.drawable.main);
		canvas.drawBitmap(bgMain, 0, 0, mPaint);
		mPaint.setStyle(Style.STROKE);
		mPaint.setStrokeWidth(2);
		for (int i = 0; i < MENU_RECT_LENGTH; i++)
		{
			canvas.drawRect(menuRect[i], mPaint);
		}
		;
		canvas.drawRect(rectNewGame, mPaint);
		
	}
	
	private void drawHELP(Canvas canvas)
	{

		// 画出 帮助 界面的背景
		Bitmap bgHelp = MarsRuntime.loadBitmap(R.drawable.help);
		canvas.drawBitmap(bgHelp, 0, 0, mPaint);
		// mPaint.setAntiAlias(true);
	}
	
	private void drawABOUT(Canvas canvas)
	{

		// 画出关于界面的背景
		Bitmap bgAbout = MarsRuntime.loadBitmap(R.drawable.stage1_bg);
		canvas.drawBitmap(bgAbout, 0, 0, mPaint);
		// mPaint.setAntiAlias(true);
	}
	
	private void drawSET(Canvas canvas)
	{

		// 画出设置界面的背景
		Bitmap bgSet = MarsRuntime.loadBitmap(R.drawable.setting);
		canvas.drawBitmap(bgSet, 0, 0, mPaint);
		mPaint.setStyle(Style.STROKE);
		mPaint.setStrokeWidth(2);
		for (int i = 0; i < SETTING_MENU_RECT; i++)
		{
			canvas.drawRect(SettingMenuRect[i], mPaint);
		}
		;
	}
	
	private void drawNEWGAMEDAYTIME(Canvas canvas)
	{

		// 循环白天背景
		canvas.drawBitmap(gameBg, bg_x, 0, mPaint);
		canvas.drawBitmap(gameBg2, bg_x2, 0, mPaint);
		// 循环云彩
		canvas.drawBitmap(gameBgCloud, bg_x_c, 0, mPaint);
		canvas.drawBitmap(gameBgCloud2, bg_x2_c, 0, mPaint);
	}
	
	private void drawNEWGAMENIGHT(Canvas canvas)
	{

		// 循环背景夜晚
		canvas.drawBitmap(gameBgNight, bg_x, 0, mPaint);
		canvas.drawBitmap(gameBgNight2, bg_x2, 0, mPaint);
		canvas.drawBitmap(gameBgNIGHTTREE, bg_x_c, 0, mPaint);
		canvas.drawBitmap(gameBgNIGHTTREE2, bg_x2_c, 0, mPaint);
	}
	
	private void drawNewDusk(Canvas canvas)
	{

		// 循环黄昏背景
		canvas.drawBitmap(gameBgDusk, bg_x, 0, mPaint);
		canvas.drawBitmap(gameBgDusk2, bg_x2, 0, mPaint);
		canvas.drawBitmap(gameBgDuskTREE, bg_x_c, 0, mPaint);
		canvas.drawBitmap(gameBgDuskTREE2, bg_x2_c, 0, mPaint);
		
	}
	
	private void drawSave(Canvas canvas)
	{

		// 画出保存界面的背景
		Bitmap bgSave = MarsRuntime.loadBitmap(R.drawable.save);
		canvas.drawBitmap(bgSave, 0, 0, mPaint);
		mPaint.setStyle(Style.STROKE);
		mPaint.setStrokeWidth(2);
		for (int i = 0; i < SAVE_MENU_RECT; i++)
		{
			canvas.drawRect(SAVEMenuRectLeft[i], mPaint);
		}
		
		mPaint.setStyle(Style.STROKE);
		mPaint.setStrokeWidth(2);
		for (int i = 0; i < SAVE_MENU_RECT; i++)
		{
			canvas.drawRect(SAVEMenuRectRight[i], mPaint);
		}
		;
		
	}
	
	private void back()
	{

		if (state == MAIN)
		{
			MarsRuntime.getRuntime().quit();
		}
		else
		{
			state = MAIN;
		}
	}
	
	private int resIndex = R.drawable.help;
	
	@Override
	public void tick()
	{

		switch (state)
		{
			case LOAD_RES:
				doDrawLoadbg();
				break;
			case MAIN:

				break;
			
			case HELP:

				break;
			
			case ABOUT:
				break;
			
			case SET:
				break;
			
//			case NEW_GAME_DAYTIME:
//				doDrawNewGameDaytime();
//				break;
//			case NEW_GAME_NIGHT:
//				doDrawNewGameNight();
//				break;
//			case NEW_GAME_DUSK:
//				doDrawNewGameDusk();
//				break;
		}// switch();
		
	}// tick();
	
	private void doDrawLoadbg()
	{

		// 控制loading后面的"...."
		if (miCount < 100)
		{
			miCount++;
		}
		else
		{
			miCount = 0;
		}
		end = STR_LOAD.length() - 4 + miCount % 4;
		if (resIndex < R.drawable.win4)
		{
			MarsRuntime.loadBitmap(resIndex);
			rectFill.right += POS_LODING_W / (R.drawable.win4 - R.drawable.help);
			resIndex++;
			// System.out.println("rectFill.right == " +
			// rectFill.right);
		}
		else
		{
			rectFill.right = POS_LODING_X + POS_LODING_W - LOADING_BOARD_WIDTH;
			state = MAIN;
		}
		;
		
	}
	
	// --------------------画出白天循环场景-------------------------------
	private void doDrawNewGameDaytime()
	{

		if (gameBg == null || gameBg2 == null)
		{
			return;
		}
		else
		{
			final int bgwidth = gameBg.getWidth();
			if (bg_x > -bgwidth)
			{
				bg_x -= BG_SPEED;
				bg_x2 -= BG_SPEED;
				bg_x_c -= BGCLOUD_SPEED;
				bg_x2_c -= BGCLOUD_SPEED;
			}
			
			else
			{
				bg_x = bg_x2;
				bg_x2 = bgwidth;
				bg_x_c = bg_x2_c;
				bg_x2_c = bgwidth;
			}
		}
		
	}
	
	// --------------------画出夜晚循环场景-------------------------------
	private void doDrawNewGameNight()
	{

		if (gameBgNight == null || gameBgNight2 == null)
		{
			return;
		}
		else
		{
			final int bgwidth = gameBgNight.getWidth();
			if (bg_x > -bgwidth)
			{
				bg_x -= BG_SPEED;
				bg_x2 -= BG_SPEED;
				bg_x_c -= BGCLOUD_SPEED;
				bg_x2_c -= BGCLOUD_SPEED;
			}
			
			else
			{
				bg_x = bg_x2;
				bg_x2 = bgwidth;
				bg_x_c = bg_x2_c;
				bg_x2_c = bgwidth;
			}
		}
	}
	
	// --------------------画出黄昏循环场景-------------------------------
	private void doDrawNewGameDusk()
	{

		if (gameBgDusk == null || gameBgDusk2 == null)
		{
			return;
		}
		else
		{
			final int bgwidth = gameBgDusk.getWidth();
			if (bg_x > -bgwidth)
			{
				bg_x -= BG_SPEED;
				bg_x2 -= BG_SPEED;
				bg_x_c -= BGCLOUD_SPEED;
				bg_x2_c -= BGCLOUD_SPEED;
			}
			
			else
			{
				bg_x = bg_x2;
				bg_x2 = bgwidth;
				bg_x_c = bg_x2_c;
				bg_x2_c = bgwidth;
			}
		}
	}
	
	@Override
	public void onTouchEvent(MotionEvent event)
	{

		int x = (int) event.getX();
		int y = (int) event.getY();
		System.out.println("gameUI onTouchEvent " + x + ";" + y);
		switch (state)
		{
			case MAIN:
				// 标题界面的选择菜单
				onTouchMain(event, x, y);
				break;
			case SAVE:
				onTouchSave(event, x, y);
			default:
				break;
		}
		
	}
	
	private void onTouchSave(MotionEvent event, int x, int y)
	{

		// Loading/save界面的选择
		if (event.getAction() == MotionEvent.ACTION_UP)
		{
			
			if (Tools.pointInRect(x, y, SAVEMenuRectLeft[FIRST_STAG]))
			{
//				stage = 1;
//				enterPlay();
//				changeState(NEW_GAME_DAYTIME);
//				state = NEW_GAME_DAYTIME;
			}
			else if (Tools.pointInRect(x, y, SAVEMenuRectLeft[SECOND_STAG]))
			{
//				changeState(NEW_GAME_NIGHT);
//				state = NEW_GAME_NIGHT;
			}
			else if (Tools.pointInRect(x, y, SAVEMenuRectLeft[THIRD_STAG]))
			{
//				changeState(NEW_GAME_DUSK);
//				state = NEW_GAME_DUSK;
			}
			
		}
		
	}
	
	private void onTouchMain(MotionEvent event, int x, int y)
	{

		if (event.getAction() == MotionEvent.ACTION_UP)
		{
			
			if (Tools.pointInRect(x, y, menuRect[CHOOSELOAD]) && haveSave)
			{
				changeState(SAVE);
			}
			else if (Tools.pointInRect(x, y, menuRect[CHOOSESETTING]))
			{
				state = SET;
			}
			else if (Tools.pointInRect(x, y, menuRect[CHOOSEQUIT]))
			{
				back();
			}
			else if (Tools.pointInRect(x, y, rectNewGame))
			{
				// state = NEW_GAME_DAYTIME;
//				changeState(NEW_GAME_DAYTIME);
				enterPlay(1);
			}
			
		}
		else if (event.getAction() == MotionEvent.ACTION_UP)
		{
			
		}
	}
	
	private int stage;
	public void enterPlay(int stage)
	{

		MarsRuntime.getRuntime().changeScreen(gp, GamePlay.MAIN, new Integer(stage));
	}
	
	
	private boolean haveSave;

	
	private void changeState(int state)
	{

		// if(Tools.load())
		// {
		// state = SAVE;
		// }
		// else{
		// //没有进度可以选
		//			
		// }
		switch (state)
		{
			case MAIN:
				if (Tools.load())
				{
					String strStage = (String) Tools.pro.get(Tools.STAGE);
					this.stage = Integer.getInteger(strStage);
					if (this.stage > 0)
					{
						haveSave = true;
					}
					else
					{
						haveSave = false;
					}
				}
				else
				{
					// 没有进度可以选
					haveSave = false;
				}
				this.state = MAIN;
				break;
			case LOAD_RES:
				this.state = LOAD_RES;
				break;
			case SAVE:
				this.state = SAVE;
				break;
			case HELP:
				this.state = HELP;				
				break;
			
//			case NEW_GAME_DAYTIME:
//				this.state = NEW_GAME_DAYTIME;
//				break;
//			case NEW_GAME_DUSK:
//				this.state = NEW_GAME_DUSK;
//				break;
//			case NEW_GAME_NIGHT:
//				this.state = NEW_GAME_NIGHT;
//				break;
		}
	}
	
	// -----------------------------------------------------------
	@Override
	public void focusIn(int entryPoint, Object[] args)
	{

	}
	
	@Override
	public void focusOut()
	{

	}
	
	@Override
	public void releaseRes()
	{

	}
	
	@Override
	public void onKeyDown(int keyCode, KeyEvent event)
	{

	}
	
	@Override
	public void onKeyUp(int keyCode, KeyEvent event)
	{

		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			this.back();
		}
		
	}
	

	
}
